<?php 
class PointFormsController extends AppController{
	var $name = "PointForms";
	var $uses = array('PointField', 'PointFieldDetail', 'UnionMemberReview');
	var $components = array('RequestHandler');
	var $helpers = array('Js' => array('Jquery'), 'Ajax');
	function add(){
		if($_POST){
			foreach ($_POST['mainForm']['mainForm'] as $pointfield){
				$pfname = $pointfield['pfname'];
				$data=array('PointField'=>array('name'=>$pointfield['pfname']), 
					'PointFieldDetail'=>$pointfield['pfdetailtemplate']); 
				$this->PointField->saveAll($data);
			}
		}
	}
	function edit($year){
		if($_POST){
			foreach ($_POST['mainForm']['mainForm'] as $pointfield){
				$pfname = $pointfield['pfname'];
				$data=array('PointField'=>array('id'=> $pointfield['pfid'],'name'=>$pointfield['pfname'], 'year'=>$year), 
					'PointFieldDetail'=>$pointfield['pfdetailtemplate']);
				$this->PointFieldDetail->deleteAll(array('PointFieldDetail.pf_id'=>$pointfield['pfid'])); 
				$this->PointField->saveAll($data);
				//var_dump($data);
			}
			
		}
		else{
			$this->set('data', $this->PointField->find('all', array('conditions'=>array('PointField.year'=>$year))));
		}
	}
	function index(){
		$this->set('pointforms', $this->PointField->query('Select distinct(PointField.year) from point_fields as PointField'));
	}
	function preview($year){
		$this->set('year', $year);
		$this->set('data', $this->PointField->find('all', array('conditions'=>array('PointField.year'=>$year))));
	}
	function estimate(){
		$year = date('Y');
		$current_user = $this->Session->read('Auth.UnionMember');
		$current_user_id = $current_user['id'];
		if(empty($_POST)){
			$this->set('year', $year);
			$data= $this->PointField->find('all', array('conditions'=>array('PointField.year'=>$year)));
			$this->set('data', $data);
			/*$list_pf_id = array();
			foreach ($data as $pf) {
				array_push($list_pf_id, $pf['PointField']['id']);
			}
			$reviewdata = $this->UnionMemberReview->find('all', array('conditions'=>array('um_id'=>$current_user_id, 'pf_id'=>$list_pf_id)));
			$this->set('reviewdata', $reviewdata);
			var_dump($reviewdata);*/
		}
		else{
			  
			foreach ($_POST['form'] as $pf_id=>$pfdetail){
				$data = array('UnionMemberReview'=>array('id'=>$pfdetail['review_id'] ,'um_id'=>$current_user_id, 'pf_id'=>$pf_id, 'pfd_id'=>$pfdetail['PointFieldDetail']));
				$this->UnionMemberReview->saveAll($data);
			}
			$this->flash(__('Bạn đã hoàn tất bảng đánh giá rèn luyện đoàn viên, xem lại', true), array('action' => 'estimate'));
		}
	}
	function getReview($year, $um_id, $pf_id){
		$temp = $this->UnionMemberReview->find('first', array('conditions'=>array('PointField.year'=>$year, 'UnionMemberReview.um_id'=>$um_id, 'UnionMemberReview.pf_id'=>$pf_id)));
		return $temp;
	}
	function review($year, $um_id){
		$this->set('year', $year);
		$this->set('um_id', $um_id);
		$data= $this->PointField->find('all', array('conditions'=>array('PointField.year'=>$year)));
		$this->set('data', $data);
	}
}
?>